package com.kongchengji.spider.picServer;


import com.kongchengji.spider.station.domain.PicJsonDO;
import com.kongchengji.spider.util.HttpClientPool;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import java.io.*;
import java.util.zip.GZIPOutputStream;



public class ReturePicByteToServer {
    public static void reterData(PicJsonDO picJsonDO,String savePicBase64Url){
        GZIPOutputStream gzip = null;
        HttpPost httpPost = null;
        try {
            httpPost = new HttpPost(savePicBase64Url);
            HttpClient client = HttpClientPool.getHttpClient();
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            byte[] bytes = picJsonDO.toString().getBytes("utf-8");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            gzip = new GZIPOutputStream(bos);
            gzip.write(bytes);
            gzip.finish();
            builder.addBinaryBody("picJsonDO",bos.toByteArray());
            HttpEntity httpEntity = builder.build();
            httpPost.setEntity(httpEntity);
            client.execute(httpPost);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(httpPost != null){
                httpPost.releaseConnection();
            }
            try {
                if(gzip != null){
                    gzip.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
